home *** CD-ROM | disk | FTP | other *** search
- Path: ausnews.austin.ibm.com!usenet
- From: Leo Uzcategui <leou@austin.ibm.com>
- Newsgroups: comp.lang.c++
- Subject: STL: for_each vs transform
- Date: Thu, 29 Feb 1996 12:53:37 -0600
- Organization: IBM Corp.
- Message-ID: <3135F631.41C6@austin.ibm.com>
- NNTP-Posting-Host: socks.austin.ibm.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; AIX 1)
-
- How can iterate thru a container, using for_each, and apply a function
- object which modifies each of the elements in the container?
- I tried passing the argument by name, but the template definition
- will not allow it.
-
- Here's the example: Note addr and size values never change.
-
- #include <iostream.h>
- #include <algo.h>
- #include <vector.h>
-
- struct S { vector<int> v; };
- struct g : public binary_function<S,int,int> {
- int operator()(S s,int x) const
- {s.v.push_back(x);
- cout<<"x="<<x<<endl;
- cout<<"g() addr of s:"<<(size_t)&s<<" size of v:"<<s.v.size()<<endl;
- return 0;};
- };
-
- void main() {
- vector<S> A(3); // A has 3 vector<int>'s
- for (int i=0; i<2; i++)
- for_each( A.begin(),A.end(), bind2nd(g(),i) );
- }
-